package com.honglang.rss.impl;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import com.honglang.common.tools.HtmlCodeProcess;
import com.honglang.module.article.entity.BlogArticle;

public class RSSUtil {
	
	
	public static Channel createChannel(List<BlogArticle> blogArticleList,String urlPrefix){
		
		Channel channel = new Channel("红狼 Blog",urlPrefix,"RedWolf Blog");
		TimeZone tz = TimeZone.getTimeZone("GMT:00");
		DateFormat dfGMT = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US);
	    dfGMT.setTimeZone( tz );
		Calendar calendar = Calendar.getInstance(tz);
		channel.setCopyright("Copyright ©" + calendar.get(Calendar.YEAR)+ " honglang All Rights Reserved");
		channel.setLanguage("zh-cn");
		channel.setPubDate(dfGMT.format(new Date()));
		channel.setLastBuildDate(dfGMT.format(new Date()));
		for(BlogArticle blogArticle : blogArticleList){
			channel.addItem((RSSUtil.createItem(blogArticle, urlPrefix)));
		}
		return channel;
	}
	
	
	
	public static Item createItem(BlogArticle blogArticle,String urlPrefix){
		Item item = new Item();
		item.setTitle(blogArticle.getArticle().getTitle());
		item.setCategory(blogArticle.getArticle().getArticleCategories().getCategoryName());
		item.setLink(urlPrefix+"/article/articleDetail?bid="+blogArticle.getId().toString());
		item.setGuid(urlPrefix+"/article/articleDetail?bid="+blogArticle.getId().toString());
		item.setPubDate(blogArticle.getArticle().getWriteDate().toString());
		item.setDescription(HtmlCodeProcess.clearHtmlTag(blogArticle.getArticle().getArticlePreView()).replace("\n",""));
		item.setAuthor("honglangqq@gmail.com");
		return item;
	}
	
	
}
